<?php
/**
 * Created by PhpStorm.
 * User: freethy
 * Date: 2018-05-11
 * Time: 9:49
 */

namespace app\api;
use think\cache\driver\Redis;
use think\Request;

class Auth
{
    public function run(&$params)
    {
        //获取请求信息
        $request = Request::instance();
        $action = $request->action();

        // 特殊 提示文字也需要token验证,get请求不需要token
        if($action != 'tipmsg' && $request->isGet() || $action == 'up') return;
        //登陆不需要token验证
        if($action != 'login' && $action != 'activationaccount' && $action != 'register') {
            //如果缺少token,则返回失效
            if(!isset($_SERVER['HTTP_TOKEN'])){
                echo  json_encode(['code'=>402,'msg'=>getMsg('缺少token参数,请填写!')]);
                die;
            }
            //获取当前用户token
            $token = $_SERVER['HTTP_TOKEN'];
            $redis = new Redis();
            $userInfo = $redis->get($token);
            if(empty($userInfo)) //如果没有该token的信息,则返回
            {
                echo  json_encode(['code'=>403,'msg'=>getMsg('登录信息已过期,请重新登陆')]);
                die;
            }
        }

    }
}